#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# 切片
def demo():
    students = [1,2,34,54,7,8,89,233]
    print(students[0:3]) # [1, 2, 34]
    # 如果第一个为0，则可以省略
    print(students[:3]) # [1, 2, 34]
    print(students[3:3]) # []
    print(students[3:5]) # [54, 7]
    # 倒数切片
    print(students[-5:-3]) # [54, 7]
    # 只能由小到大
    print(students[-3:-5]) # []
    # 什么都不写，则原样复制一个
    print(students[:]) # [1, 2, 34, 54, 7, 8, 89, 233]
    # 每几个取一个
    print(students[::2]) # [1, 34, 7, 89]

# demo()

# 案例：
def trim(str):
    begin = str[:1]
    end = str[len(str)-1:len(str)]
    if begin == ' ':
        str = str[1:len(str)]
    if end == ' ':
        str = str[:len(str)-1]
    if begin != ' ' and end != ' ':
        return str
    else:
        return trim(str)

# 测试:
def test():
    if trim('hello  ') != 'hello':
        print('测试失败!')
    elif trim('  hello') != 'hello':
        print('测试失败!')
    elif trim('  hello  ') != 'hello':
        print('测试失败!')
    elif trim('  hello  world  ') != 'hello  world':
        print('测试失败!')
    elif trim('') != '':
        print('测试失败!')
    elif trim('    ') != '':
        print('测试失败!')
    else:
        print('测试成功!')

test()